﻿using System;
using System.Linq;
using System.ComponentModel;
using System.Windows.Data;
using System.Collections.ObjectModel;

using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;

using CatalogModule.Models;
using CatalogModule.Services;
using ServiceAgent;
using ServiceAgent.CatalogueService;
using Infrastructure;
using Infrastructure.Helpers;
using Infrastructure.Events;
using System.Windows.Input;
using System.Windows;
using Microsoft.Practices.Unity;
using Microsoft.Practices.ServiceLocation;
using CatalogModule.Events;
using Infrastructure.InteractionRequests;
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;

namespace CatalogModule.ViewModels
{
    /// <summary>
    /// ViewModel for AddLopView.
    /// </summary>
    public class AddLopViewModel : NotificationObject, INavigationAware, IPopupWindowActionAware
    {
        #region Fields
        IUnityContainer container;
        IEventAggregator eventAggregator;
        string tenGoi;
        string maLop;
        string moTa;
        ObservableCollection<DonViDTO> donViList;
        ObservableCollection<KhoaHocDTO> khoaHocList;
        DonViDTO currentDonVi;
        KhoaHocDTO currentKhoaHoc;


        #endregion

        #region Constructor
        /// <summary>
        /// Default constructor.
        /// </summary>
        public AddLopViewModel()
        {
            this.container = ServiceLocator.Current.GetInstance<UnityContainer>();
            this.eventAggregator = ServiceLocator.Current.GetInstance<EventAggregator>();
            this.Initialize();
            
        }
        #endregion

        #region Administrative Properties
        // IPopupWindowActionAware implementation
        public Window HostWindow { get; set; }

        public Notification HostNotification { get; set; }

        public string TenGoi
        {
            get { return tenGoi; }
            set
            {
                tenGoi = value;
                RaisePropertyChanged(() => this.TenGoi);
            }
        }

        public string MaLop
        {
            get { return maLop; }
            set
            {
                maLop = value;
                RaisePropertyChanged(() => this.MaLop);
            }
        }

        public string MoTa
        {
            get { return moTa; }
            set
            {
                moTa = value;
                RaisePropertyChanged(() => this.MoTa);
            }
        }

        public ObservableCollection<DonViDTO> DonViList
        {
            get { return donViList; }
            set
            {
                donViList = value;
            RaisePropertyChanged(() => this.DonViList);
            }
        }

        public ObservableCollection<KhoaHocDTO> KhoaHocList
        {
            get { return khoaHocList; }
            set
            {
                khoaHocList = value;
            RaisePropertyChanged(() => this.KhoaHocList);
            }
        }

        public DonViDTO CurrentDonVi
        {
            get { return currentDonVi; }
            set
            {
                currentDonVi = value;
                RaisePropertyChanged(() => this.CurrentDonVi);
            }
        }

        public KhoaHocDTO CurrentKhoaHoc
        {
            get { return currentKhoaHoc; }
            set
            {
                currentKhoaHoc = value;
                RaisePropertyChanged(() => this.CurrentDonVi);
            }
        }
        #endregion

        #region Command Properties
        public ICommand AddNewCommand { get; private set; }
        public ICommand CancelCommand { get; set; }
        #endregion

        #region Private Methods
        private void Initialize()
        {
            this.AddNewCommand = new DelegateCommand(this.ExecuteAddNew);
            this.CancelCommand = new DelegateCommand(this.ExecuteCancel);

            this.GetDonViList();
            this.GetKhoaHocList();
        }

        void GetDonViList()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    DonViList = proxy.GetDonViListByCapDonVi();
                }
            }
            catch (Exception)
            {

            }
        }

        void GetKhoaHocList()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    KhoaHocList = proxy.GetKhoaHocList();
                }
            }
            catch (Exception)
            {

            }
        }

        void ExecuteAddNew()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    LopDTO obj = new LopDTO();
                    obj.TenGoi = this.TenGoi;
                    obj.MaLop = this.MaLop;
                    obj.DonViID = this.CurrentDonVi.ID;
                    obj.KhoaHocID = this.CurrentKhoaHoc.ID;
                    obj.MoTa = this.MoTa;
                    proxy.SaveLop(obj);
                    this.eventAggregator.GetEvent<AddLopEvent>().Publish(obj);
                    if (this.HostWindow != null)
                    {
                        this.HostWindow.Close();
                    }
                }
            }
            catch (System.Exception)
            {
                MessageBox.Show("Có lỗi");
            }
        }

        void ExecuteCancel()
        {
            this.eventAggregator.GetEvent<AddLopEvent>().Publish(null);

            if (this.HostWindow != null)
            {
                this.HostWindow.Close();
            }
        }
        #endregion

        #region INavigationAware Members
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            // Called to see if this view can handle the navigation request.
            // If it can, this view is activated.
            return false;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

            // Called when this view is deactivated as a result of navigation to another view.
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            //// Called to initialize this view during navigation.

            //// Retrieve any required paramaters from the navigation Uri.
            //string id = navigationContext.Parameters["ID"];
            //if (!string.IsNullOrEmpty(id))
            //{
            //    DataItem item = _dataService.GetModel().FirstOrDefault(dataItem => dataItem.Id == id);
            //}
        }
        #endregion
    }
}
